package com.corpgovernment.common.supplier;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * ResponseConverter的持有者
 *
 * @author xukang zhang
 * @date 2024/2/8 13:58
 */
public class ResponseConverterHolder {

    private final Map<Class<?>, ResponseConverter<?>> responseConverterMap = new HashMap<>();

    /**
     * 注册ResponseConverter
     *
     * @param responseConverter
     */
    public void register(ResponseConverter<?> responseConverter) {
        if (responseConverter == null) {
            return;
        }
        responseConverterMap.put(responseConverter.getClazz(), responseConverter);
    }

    /**
     * 获取指定Class的ResponseConverter
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> ResponseConverter<T> getResponseConverter(Class<T> clazz) {
        return (ResponseConverter<T>) responseConverterMap.get(clazz);
    }
}
